package com.app.core_app.controller;

import com.app.core_app.dto.CreateCaseDto;
import com.app.core_app.dto.FileUploadDto;
import com.app.core_app.entity.CaseManager;
import com.app.core_app.properties.MypwProperties;
import com.app.core_app.service.CaseManagerService;
import com.app.public_dependency.exception.GlobalException;
import com.app.public_dependency.response.Result;
import com.app.public_dependency.xlsx.ExcelFileUtil;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.ReusableMessage;
import org.apache.xmlbeans.impl.xb.xsdschema.FieldDocument;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import java.io.File;
import java.io.IOException;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

@RestController
@RequestMapping("create-test")
@RequiredArgsConstructor
@Slf4j
public class CreateTestController {

    private final CaseManagerService caseManagerService;

    private final MypwProperties mypwProperties;

    /**
     * 全查询PgId
     *
     * @return
     */
    @GetMapping("queryPgId")
    public Result queryPgId() {
        List<String> pgIdList = caseManagerService.queryPgId();
        return Result.success().data("pgIdList", pgIdList);
    }

    /**
     * 根据PgId 查询ProcessId
     *
     * @return
     */
    @GetMapping("queryProcessId/{pgId}")
    public Result queryProcessId(@PathVariable String pgId) {
        List<String> processIdList = caseManagerService.queryProcessId(pgId);
        return Result.success().data("processIdList", processIdList);
    }

    /**
     * 根据PgId,ProcessId 查询CaseId
     *
     * @return
     */
    @GetMapping("queryCaseId/{pgId}/{processId}")
    public Result queryCaseId(@PathVariable String pgId, @PathVariable String processId) {
        List<String> caseIdList = caseManagerService.queryCaseId(pgId, processId);
        return Result.success().data("caseIdList", caseIdList);
    }


    @PostMapping("fileUpload")
    public Result fileUpload(FileUploadDto fileUploadDto) {
        log.info("fileUploadDto: " + fileUploadDto);
        FilePart file = fileUploadDto.getFile();
        Path path = Path.of(mypwProperties.getPwTempDataPath(), fileUploadDto.getPgId(), fileUploadDto.getProcessId(), fileUploadDto.getCaseId());
        log.info("path: " + path);
        Path filePath = path.resolve(file.filename());
        log.info("filePath.toUri(): " + filePath.toUri());
        File tempFile = new File(filePath.toUri());
        File oldFile = new File(path.toUri());
        if (!oldFile.exists()) {
            if (!oldFile.mkdirs()) {
                log.error(path + "路径不存在，创建失败");
                return Result.error(500, "路径不存在，创建失败");
            }
        }
        ExcelFileUtil.CreateEmptyExcelFile(filePath.toString());
        Mono<Void> mono = file.transferTo(filePath);
        return Result.success();
    }

    @PostMapping("handleCreate")
    public Result handleCreate(@RequestBody CreateCaseDto createCaseDto) {
        log.info(createCaseDto.toString());
        return Result.success();
    }

}
